//
//  RealNameAuthenticationViewController.m
//  联付商圈
//
//  Created by 于洋 on 2017/7/19.
//  Copyright © 2017年 lianfu. All rights reserved.
//

#import "RealNameAuthenticationViewController.h"

@interface RealNameAuthenticationViewController ()<UITextFieldDelegate>

{
    UITextField *nameTF;
    UITextField *IDTF;
}

@end

@implementation RealNameAuthenticationViewController

- (void)viewDidLoad {
    [super viewDidLoad];    
    self.observeKeyboard = YES;
    
    self.navigationController.navigationBar.hidden = NO;
    self.view.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.00];
    [self setUpNav];
    [self makeUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)setUpNav{

    self.navigationItem.title = @"实名认证";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}

- (void)makeUI{

    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, 250)];
    backView.backgroundColor = [UIColor whiteColor];
    [self backViewAddSubView:backView];
    [self.view addSubview:backView];
    
    UILabel *nameLb = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
    nameLb.text=@"  姓名";
    nameLb.textColor = [UIColor lightGrayColor];
    nameLb.textAlignment = NSTextAlignmentLeft;
    nameLb.userInteractionEnabled = YES;
    
    
    UILabel *IDLb = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
    IDLb.text=@"  身份证号";
    IDLb.textColor = [UIColor lightGrayColor];
    IDLb.textAlignment = NSTextAlignmentLeft;
    
    
    nameTF = [[UITextField alloc]initWithFrame:CGRectMake(0, backView.maxY+20, SCREENW, 50)];
    nameTF.placeholder = @"请输入您的姓名";
    nameTF.backgroundColor = [UIColor whiteColor];
    nameTF.leftView = nameLb;
    nameTF.delegate =self;
    nameTF.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:nameTF];
    
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(10, nameTF.maxY, SCREENW, 0.5)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:lineView];
    
    IDTF = [[UITextField alloc]initWithFrame:CGRectMake(0, lineView.maxY, SCREENW, 50)];
    IDTF.placeholder = @"请输入您的身份证号";
    IDTF.backgroundColor = [UIColor whiteColor];
    IDTF.leftView = IDLb;
    IDTF.delegate = self;
    IDTF.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:IDTF];
    
    
    UIButton *rightNowAuthenBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, SCREENH-150, SCREENW - 20, 50)];
    rightNowAuthenBtn.backgroundColor =[UIColor colorWithRed:0.23 green:0.62 blue:0.87 alpha:1.00];
    [rightNowAuthenBtn setTitle:@"立即认证" forState:UIControlStateNormal];
    [rightNowAuthenBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightNowAuthenBtn addTarget:self action:@selector(rightNowAuthenBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:rightNowAuthenBtn];
}
- (void)backViewAddSubView:(UIView *)backView{

    UIButton *imageViewBtn = [[UIButton alloc]initWithFrame:CGRectMake((SCREENW-100)/2, 94, 100, 100)];
    imageViewBtn.layer.cornerRadius = 50;
    [imageViewBtn setImage:[UIImage imageNamed:@"头像"] forState:UIControlStateNormal];
    [backView addSubview:imageViewBtn];
     
     UILabel *sureRenZhengLb = [[UILabel alloc]initWithFrame:CGRectMake(0, imageViewBtn.maxY+10, SCREENW, 40)];
    sureRenZhengLb.text = @"未实名认证";
    sureRenZhengLb.textAlignment = NSTextAlignmentCenter;
    sureRenZhengLb.font = [UIFont systemFontOfSize:15.0];
    sureRenZhengLb.textColor = [UIColor blackColor];
    [backView addSubview:sureRenZhengLb];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self.view endEditing:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    [self.view endEditing:YES];
    return YES;
}

- (void)rightNowAuthenBtnClick{

    if ([nameTF.text isEqualToString:@""]) {
        
        MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
        HUD.labelText = @"请填写姓名";
        HUD.mode = MBProgressHUDModeText;
        [HUD hide:YES afterDelay:1.0];//
    }else if ([IDTF.text isEqualToString:@""]){
    
        MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
        HUD.labelText = @"请填写身份证号";
        HUD.mode = MBProgressHUDModeText;
        [HUD hide:YES afterDelay:1.0];//
    }else{
        NSDictionary *param = @{@"name" : _userModel.userName, @"mingzi" : nameTF.text, @"sfzh" : IDTF.text};
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain", nil];
        manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        manager.securityPolicy.allowInvalidCertificates = YES;
        
        [manager POST:[Host stringByAppendingString:@"sfzh?"] parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
            if (dic[@"success"]) {
                [self.navigationController popViewControllerAnimated:YES];
            } else {
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        }];
        

    }
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
